//
//  StartTwoViewController.m
//  yinsiwenzhen
//
//  Created by Yangfan on 14/12/24.
//  Copyright (c) 2014年 4gread. All rights reserved.
//

#import "StartTwoViewController.h"

@interface StartTwoViewController ()

@end

@implementation StartTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"第二步";
    self.navigationItem.backBarButtonItem = backItem;
    
    if (isiphone6plus) {
        _destributionWord.font = [UIFont systemFontOfSize:24];
        _navigationTitle.font = [UIFont systemFontOfSize:20];
    } else {
        _destributionWord.font = [UIFont systemFontOfSize:20];
        _navigationTitle.font = [UIFont systemFontOfSize:19];
    }
    dateArr = @[@"1天", @"2天", @"3天", @"4天", @"5天", @"6天", @"> 1周", @"> 2周", @"> 1月", @"> 6月", @"一年", @"三年", @"常年"];
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isiphone6plus) {
        return 44;
    } else {
        return 36;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (dateArr != nil && dateArr.count > 0) {
        return dateArr.count;
    } else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"twoViewControllerCell"];
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    
    
    // 重用机制，对于已经重用的view有影响
    [(UILabel *)[cell viewWithTag:1] setTextColor:HexRGB(0xAFAEAE)] ;
    [[cell viewWithTag:2] removeFromSuperview];
    
    
    UILabel *title = (UILabel *)[cell viewWithTag:1];
    title.text = [dateArr objectAtIndex:indexPath.row];
    if (isiphone6plus) {
        title.font = [UIFont systemFontOfSize:22];
    } else {
        title.font = [UIFont systemFontOfSize:18];
    }
    
    UIImageView *imageSelected = [[UIImageView alloc] init];
    if (isiphone6plus) {
        [imageSelected setFrame:CGRectMake(105, 0, 17, 44)];
    } else {
        [imageSelected setFrame:CGRectMake(105, 0, 14, 36)];
    }
    imageSelected.tag = 2;
    [imageSelected setHidden:YES];
    [imageSelected setImage:[UIImage imageNamed:@"selected"]];
    [cell.contentView addSubview:imageSelected];
    
    // 重用机制，如果选中的行正好要重用
    NSLog(@"****%d", [dateArr indexOfObject:_listView.howLong]);
    if (_listView.howLong != nil) {
        preIndex = [dateArr indexOfObject:_listView.howLong];
    }
    if (preIndex == indexPath.row) {
        [title setTextColor:HexRGB(0x60cbfd)];
        [imageSelected setHidden:NO];
    } else {
    }

    return cell;
}

//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    UILabel *title = (UILabel *)[cell viewWithTag:1];
//    [title setTextColor:HexRGB(0xAFAEAE)];
//    [[cell viewWithTag:2] setHidden:YES];
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:preIndex inSection:0];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndex];
    UILabel *title = (UILabel *)[cell viewWithTag:1];
    [title setTextColor:HexRGB(0xAFAEAE)];
    [[cell viewWithTag:2] setHidden:YES];
    
    
    cell = [tableView cellForRowAtIndexPath:indexPath];
    title = (UILabel *)[cell viewWithTag:1];
    [title setTextColor:HexRGB(0x60cbfd)];
    [[cell viewWithTag:2] setHidden:NO];
    
    preIndex = indexPath.row;
    
    [cell setSelected:NO];
    
    _listView.howLong = [dateArr objectAtIndex:indexPath.row];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"toThreeAction"]) {
        UIViewController *threeViewController = segue.destinationViewController;
        if ([threeViewController respondsToSelector:@selector(setListView:)]) {
            _listView.howLong = [dateArr objectAtIndex:[_mainTable indexPathForSelectedRow].row];
            [threeViewController setValue:_listView forKey:@"listView"];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
